/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server.data;

import java.text.ParseException;
import server.AuctionManager;

/**
 * Represents a Group Bid
 * @author Group 95
 */
public class Bid {
    private User bidder;
    private long auctionId;
    private double dPrice;
    private String state;

    
    public final static String OVERBID="OVERBID";
    public final static String UNDERBID="UNDERBID";
    public final static String NO_AUCTION="NO_AUCTION";
    
    final public static String USAGE="Invalid parameters, Usage: !bid <auction-id> <amount>!";
    final public static String INV_PARAMETERS="Invalid parameters, both have to be numeric!";
    
    /**
     * Bid Constructor
     * @param strParts String [] strLine[0]="!bid", strLine[1]=<auction_id>, strLine[2]=<amount> 
     * @param bidder User that did the bid
     */
    public Bid(final String [] strParts,final User bidder) throws InvalidCommandException
    {
        try 
        {
            this.auctionId = Long.parseLong(strParts[1]);
            this.dPrice = AuctionManager.getInstance().parseDouble(strParts[2]);
            this.bidder = bidder;
            this.state=null;
        }
        catch (IndexOutOfBoundsException ex) 
        {
            throw new InvalidCommandException(getErrorMessage(ex),ex);
        }
        catch (NumberFormatException ex) 
        {
            throw new InvalidCommandException(INV_PARAMETERS,ex);
        }
        catch (ParseException ex) 
        {
            throw new InvalidCommandException(INV_PARAMETERS,ex);
        }
    }
    
    /**
     * Returns a Errormessage for an Exception
     * @param cause
     * @return 
     */
    protected String getErrorMessage(final Throwable cause)
    {
        String strRet=null;
        if(cause instanceof IndexOutOfBoundsException)
        {
            strRet=USAGE;
        }
        else if((cause instanceof NumberFormatException) || (cause instanceof ParseException))
        {
            strRet=INV_PARAMETERS;
        }
        return null;
    }
    
    public long getAuctionId() {
        return auctionId;
    }

    public User getBidder() {
        return bidder;
    }

    public double getdPrice() {
        return dPrice;
    }

    public String getState() {
        return state;
    }
    
    public void setState(String strState)
    {
        this.state=strState;
    }
    
    /**
     * Compares this bid to an other bid
     * @param other
     * @return true if the price of the current bid is smaller than those of an other bid
     */
    public boolean smallerThan(Bid other)
    {
        return this.dPrice<other.dPrice;
    }
}
